// pages/desigEdit/desigEdit.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: '',
    imgApi: app.globalData.imgApi,
    fileList: [],
    id: '',
    Ddate: '请选择打版日期',
    Sdate: '请选择上架日期',
    doc: ['请选择跟单人'],
    docIndex: 0,
    isMB: false,
    brand: [],
    size: [],
    colour: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  bindDateChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getDoc()

    var that = this,
      fileList = [];
    wx.request({
      url: app.globalData.api + 'designDiagram/' + options.id,
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'GET',
      success: function(res) {
        console.log(res)
        for (let i = 0; i < res.data.data.designImage.split(',').length; i++) {
          fileList[i] = {
            uid: i,
            url: that.data.imgApi + res.data.data.designImage.split(',')[i]
          }
        }
        for (let i in that.data.doc) {
          if (that.data.doc[i].trackerName.indexOf(res.data.data.trackerName) != -1) {
            that.data.docIndex = i
          }
        }
        that.setData({
          list: res.data.data,
          fileList: fileList,
          Ddate: res.data.data.madeTemplateTime.split(' ')[0],
          Sdate: res.data.data.onlineTime.split(' ')[0],
          brand: res.data.data.brand.split(','),
          size: res.data.data.size.split(','),
          colour: res.data.data.color.split(','),
          docIndex: that.data.docIndex
        })
      },
    })
  },
  onChange(e) {
    var that = this
    console.log('onChange', e)
    const {
      file
    } = e.detail
    if (file.status === 'uploading') {
      wx.uploadFile({
        url: app.globalData.api + 'upload',
        filePath: file.url,
        name: 'file',
        success(res) {
          console.log(JSON.parse(res.data))
          that.data.fileList.push({
            uid: that.data.fileList.length,
            url: app.globalData.imgApi + JSON.parse(res.data).data
          })
          that.setData({
            fileList: that.data.fileList
          })
        }
      })
      wx.showLoading()
    } else if (file.status === 'done') {
      this.setData({
        imageUrl: file.url,
      })
    }
  },
  onSuccess(e) {
    console.log('onSuccess', e)
  },
  onFail(e) {
    console.log('onFail', e)
  },
  onComplete(e) {
    console.log('onComplete', e)
    wx.hideLoading()
  },
  onProgress(e) {
    console.log('onProgress', e)
    this.setData({
      progress: e.detail.file.progress,
    })
  },
  onPreview(e) {
    console.log('onPreview', e)
    const {
      file,
      fileList
    } = e.detail
    wx.previewImage({
      current: file.url,
      urls: fileList.map((n) => n.url),
    })
  },
  onRemove(e) {
    const {
      file,
      fileList
    } = e.detail
    console.log(e.detail)
    wx.showModal({
      content: '确定删除？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            fileList: fileList.filter((n) => n.uid !== file.uid),
          })
        }
      },
    })
  },
  upload: function(e) {
    var that = this,
      imgUrl = [];
    if (e.detail.value.styleNumber == '' || e.detail.value.brand == '' || e.detail.value.designAssistant == '' || e.detail.value.paperSampleMaster == '' || e.detail.value.styleName == '' || e.detail.value.tracker == '') {
      wx.showToast({
        title: '请填写完整信息',
      })
      return
    }
    for (var i = 0; i < this.data.fileList.length; i++) {
      imgUrl.push(this.data.fileList[i].url.substr(this.data.imgApi.length, this.data.fileList[i].url.length))
    }
    wx.request({
      url: app.globalData.api + 'designDiagram/' + this.data.id,
      data: {
        designImage: imgUrl.join(','),
        styleNumber: e.detail.value.styleNumber,
        styleName: e.detail.value.styleName,
        designAssistant: e.detail.value.designAssistant,
        tracker: e.detail.value.tracker,
        paperSampleMaster: e.detail.value.paperSampleMaster,
        madeTemplateTime: this.data.Ddate + ' 23:59:59',
        trackerId: this.data.doc[this.data.docIndex].trackerId,
        brand: this.data.brand.join(','),
        color: this.data.colour.join(','),
        size: this.data.size.join(','),
        onLineTime: this.data.Sdate + ' 23:59:59',
      },
      method: 'PUT',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  //获取跟单人
  getDoc() {
    var that = this
    app.req('designDiagram/getPurchasingManager', '', 'GET', res => {
      console.log(res)
      if (res.data.code == 200) {
        that.setData({
          doc: res.data.data
        })
      } else {
        wx.showModal({
          title: '提示',
          content: res.data.errorMsg,
        })
      }
    })
  },
  deMB() {
    this.setData({
      isMB: false
    })
  },
  add(e) {
    console.log(e)
    this.setData({
      isMB: true,
      name: e.currentTarget.dataset.name
    })
  },
  z_Submit(e) {
    switch (e.detail.target.dataset.name) {
      case '品牌':
        this.data.brand.push(e.detail.value.add)
        break
      case '尺码':
        this.data.size.push(e.detail.value.add)
        break
      case '颜色':
        this.data.colour.push(e.detail.value.add)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour,
      isMB: false
    })
  },
  z_dele(e) {
    console.log(e)
    switch (e.currentTarget.dataset.name) {
      case '品牌':
        this.data.brand.splice(e.currentTarget.dataset.index, 1)
        break
      case '尺码':
        this.data.size.splice(e.currentTarget.dataset.index, 1)
        break
      case '颜色':
        this.data.colour.splice(e.currentTarget.dataset.index, 1)
        break
    }
    this.setData({
      brand: this.data.brand,
      size: this.data.size,
      colour: this.data.colour
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})